package com.sip.stream.utils.zfy.camera;

import android.graphics.SurfaceTexture;
import android.util.Log;
import android.util.Size;
import android.view.Surface;

import com.sip.stream.media.ZFYCameraParameters;
import com.sip.stream.media.ZFYMediaManager;
import com.sip.stream.utils.zfy.ZFYCameraCallBack;
import com.sip.stream.utils.zfy.config.ZFYMediaConfig;
import io.reactivex.Observable;
import io.reactivex.Single;

public abstract class AbstractZFYCamera {
    private static final String TAG = "AbstractZFYCamera";


    public static final int FLASHMODE_ALWAYS_OFF = 2;
    public static final int FLASHMODE_ALWAYS_ON = 1;
    public static final int FLASHMODE_AUTO = 0;
    protected static final int SIZE_CAPTURE = 1;
    protected static final int SIZE_PREVIEW = 3;
    protected static final int SIZE_REMOTE = 2;
    protected static final int SIZE_VIDEO = 0;
    public static final int STATUS_CHANGESTATUS = 3;
    public static final int STATUS_CLOSE = 2;
    public static final int STATUS_OPEN = 0;
    public static final int STATUS_SETUP = 1;

    protected ZFYCameraCallBack cameraCallBack;
    protected boolean isQRScan;
    public boolean wideAngleDistortion = false;
    protected ZFYCameraParameters cameraParameters = new ZFYCameraParameters();
    protected float currentZoom = 1.0f;
    private int defaultPreviewWidth = 640;
    private int defaultPreviewHeidht = 360;
    protected boolean stabilization = true;
    protected boolean hdr = false;
    protected boolean needFaceDetect = true;
    protected boolean frontSide = false;


    public abstract Single<Boolean> capture();

    public abstract void changeInfraredLight(boolean z);

    public abstract Observable<ZFYCameraParameters> changePPI();

    public abstract Single<Boolean> closeCamera();

    public abstract String getLocalizedName();

    public abstract float getMaxZoomValue();

    public boolean isInfraredLightOpen() {
        return false;
    }

    public abstract boolean isLocked();

    public abstract boolean setFlashMode(int flashMode);

    public abstract void setInfraredLightMode(boolean lightMode);

    public abstract Single<ZFYCameraParameters> setUp();

    public void switchInfraredLight() {
    }

    public abstract void updateCameraStream(boolean isPreviewing, boolean isRecording, boolean isRemoting);

    public void updatePreviewStatus() {}

    public void setStabilization(boolean stabilization) {
        this.stabilization = stabilization;
    }

    public void setHdr(boolean hdr) {
        this.hdr = hdr;
    }

    public ZFYCameraParameters getParameters() {
        return this.cameraParameters;
    }

    public boolean isWideAngleDistortion() {
        return this.wideAngleDistortion;
    }

    public void setWideAngleDistortion(boolean wideAngleDistortion) {
        this.wideAngleDistortion = wideAngleDistortion;
    }

    public Single<Boolean> startPreviewStream() {
        return Single.just(Boolean.TRUE);
    }

    public void setCameraCallBack(ZFYCameraCallBack zFYCameraCallBack) {
        this.cameraCallBack = zFYCameraCallBack;
    }

    public void onVideoStream(byte[] bArr, long j) {
        if (this.cameraCallBack != null) {
            this.cameraCallBack.onVideoStream(bArr, j);
        }
    }

    public void onRemoteStream(byte[] bArr, long j) {
        if (this.cameraCallBack != null) {
            this.cameraCallBack.onRemoteStream(bArr, j);
        }
    }

    public void onCaptureComplete(byte[] bArr, int i) {
        if (this.cameraCallBack != null) {
            this.cameraCallBack.onCaptureComplete(bArr, i);
        } else {
            Log.w(TAG, "cameraCallBack == null");
        }
    }

    public void onCameraError(String str) {
        if (this.cameraCallBack != null) {
            this.cameraCallBack.onError(str);
        }
    }

    protected Size chooseFrontSize(Size[] sizeArr, int i) {
        if (i == 3) {
            return new Size(640, 320);
        }
        return new Size(1280, 720);
    }

    public ZFYMediaConfig getMediaConfig() {
        return ZFYMediaManager.getInstance().getConfig();
    }

    public Size chooseSize(Size[] sizeArr, int i) {
        int width = 1920;
        int height = 1080;
        if (this.isQRScan) {
            height = 1080;
            width = 1920;
        } else {
            width = 1280;
            height = 720;
        }
        if (this.frontSide) {
            height = 1080;
            width = 1920;
        } else if (i == 0) {
            width = getMediaConfig().getVideoWidth();
            height = getMediaConfig().getVideoHeight();
        } else if (i == 1) {
            width = 1920;
            height = 1080;
        } else if (i == 2) {
            width = getMediaConfig().getStreamWidth();
            height = getMediaConfig().getStreamHeight();
        } else if (i == 3) {
            width = 640;
            height = 360;
        }
        Size size = null;
        float f = height / width;
        Log.d(TAG, "defaultSize " + width + " x " + height);
        for (Size size2 : sizeArr) {
            if (size2.getWidth() == width && size2.getHeight() == height) {
                return size2;
            }
            if (size == null) {
                Log.i(TAG, "nearSize " + Math.abs(size2.getHeight() / size2.getWidth()) + " hw " + f);
                if (Math.abs((size2.getHeight() / size2.getWidth()) - f) < 0.01f) {
                    size = size2;
                }
            }
        }
        return size == null ? sizeArr[sizeArr.length - 1] : size;
    }

    public float getCurrentZoom() {
        return this.currentZoom;
    }

    public void setCurrentZoom(float currentZoom) {
        this.currentZoom = currentZoom;
    }

    public Surface getPreviewSurface() {
        SurfaceTexture previewSurfaceTexture = getPreviewSurfaceTexture();
        if (previewSurfaceTexture != null) {
            return new Surface(previewSurfaceTexture);
        }
        return null;
    }

    public SurfaceTexture getPreviewSurfaceTexture() {
        if (this.cameraCallBack != null) {
            return this.cameraCallBack.getPreviewSurfaceTexture();
        }
        return null;
    }

    public void setFrontSide(boolean frontSide) {
        this.frontSide = frontSide;
    }

    public void setQRScan(boolean isQRScan) {
        this.isQRScan = isQRScan;
    }

    public void statusChange(int status) {
        if (this.cameraCallBack != null) {
            this.cameraCallBack.cameraStatusChange(status);
        }
    }

    public void setNeedFaceDetect(boolean needFaceDetect) {
        this.needFaceDetect = needFaceDetect;
    }
}
